package com.dp.springsourcecodeanalysis.config;

import java.util.List;

import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.dp.springsourcecodeanalysis.web.converters.GoodsSkcConverters;
import com.dp.springsourcecodeanalysis.web.filters.LogInterceptor;
import com.dp.springsourcecodeanalysis.web.filters.LogPassInterceptor;
import com.dp.springsourcecodeanalysis.web.filters.LogRejectInterceptor;
import com.dp.springsourcecodeanalysis.web.messagerConverters.TempMessageConverter;

import lombok.AllArgsConstructor;

/**
 *
 * @author shala/沙拉
 * @date 2021/12/31 16:00
 */
@Component
@AllArgsConstructor
public class WebConfig implements WebMvcConfigurer {
    private final LogInterceptor logInterceptor;
    private final LogPassInterceptor passInterceptor;
    private final LogRejectInterceptor logRejectInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        WebMvcConfigurer.super.addInterceptors(registry);
        registry.addInterceptor(logInterceptor).addPathPatterns("/filter/**");
        registry.addInterceptor(passInterceptor).addPathPatterns("/filter/pass");
        registry.addInterceptor(logRejectInterceptor).addPathPatterns("/filter/reject");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        WebMvcConfigurer.super.addFormatters(registry);
        registry.addConverter(new GoodsSkcConverters());
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        WebMvcConfigurer.super.extendMessageConverters(converters);
        converters.add(new TempMessageConverter());
    }
}
